/**
 * 日期格式化工具函数
 */

/**
 * 格式化日期
 * @param date 日期对象或时间戳或日期字符串
 * @param format 格式化模板，默认为 'YYYY-MM-DD HH:mm:ss'
 * @returns 格式化后的日期字符串
 */
export function formatDate(
  date: Date | number | string,
  format = 'YYYY-MM-DD HH:mm:ss'
): string {
  const d = new Date(date)
  
  if (isNaN(d.getTime())) {
    console.error('Invalid date:', date)
    return ''
  }
  
  const year = d.getFullYear()
  const month = d.getMonth() + 1
  const day = d.getDate()
  const hour = d.getHours()
  const minute = d.getMinutes()
  const second = d.getSeconds()
  
  const formatMap: Record<string, number> = {
    'YYYY': year,
    'MM': month,
    'DD': day,
    'HH': hour,
    'mm': minute,
    'ss': second
  }
  
  return format.replace(/(YYYY|MM|DD|HH|mm|ss)/g, (match) => {
    const value = formatMap[match]
    return value < 10 ? `0${value}` : `${value}`
  })
}

/**
 * 获取相对时间描述
 * @param date 日期对象或时间戳或日期字符串
 * @param now 当前时间，默认为当前时间
 * @returns 相对时间描述，如"刚刚"、"5分钟前"、"2小时前"等
 */
export function getRelativeTime(
  date: Date | number | string,
  now: Date | number | string = new Date()
): string {
  const d = new Date(date)
  const n = new Date(now)
  
  if (isNaN(d.getTime()) || isNaN(n.getTime())) {
    console.error('Invalid date:', date, 'or now:', now)
    return ''
  }
  
  const diff = n.getTime() - d.getTime()
  const seconds = Math.floor(diff / 1000)
  const minutes = Math.floor(seconds / 60)
  const hours = Math.floor(minutes / 60)
  const days = Math.floor(hours / 24)
  const months = Math.floor(days / 30)
  const years = Math.floor(months / 12)
  
  if (seconds < 60) {
    return '刚刚'
  } else if (minutes < 60) {
    return `${minutes}分钟前`
  } else if (hours < 24) {
    return `${hours}小时前`
  } else if (days < 30) {
    return `${days}天前`
  } else if (months < 12) {
    return `${months}个月前`
  } else {
    return `${years}年前`
  }
}

/**
 * 获取日期范围
 * @param type 范围类型：'today', 'yesterday', 'thisWeek', 'lastWeek', 'thisMonth', 'lastMonth', 'thisYear', 'lastYear'
 * @returns 日期范围 [开始日期, 结束日期]
 */
export function getDateRange(
  type: 'today' | 'yesterday' | 'thisWeek' | 'lastWeek' | 'thisMonth' | 'lastMonth' | 'thisYear' | 'lastYear'
): [Date, Date] {
  const now = new Date()
  const year = now.getFullYear()
  const month = now.getMonth()
  const date = now.getDate()
  const day = now.getDay() || 7 // 将周日的0转换为7
  
  let start: Date
  let end: Date
  
  switch (type) {
    case 'today':
      start = new Date(year, month, date, 0, 0, 0)
      end = new Date(year, month, date, 23, 59, 59)
      break
      
    case 'yesterday':
      start = new Date(year, month, date - 1, 0, 0, 0)
      end = new Date(year, month, date - 1, 23, 59, 59)
      break
      
    case 'thisWeek':
      start = new Date(year, month, date - day + 1, 0, 0, 0)
      end = new Date(year, month, date + (7 - day), 23, 59, 59)
      break
      
    case 'lastWeek':
      start = new Date(year, month, date - day - 6, 0, 0, 0)
      end = new Date(year, month, date - day, 23, 59, 59)
      break
      
    case 'thisMonth':
      start = new Date(year, month, 1, 0, 0, 0)
      end = new Date(year, month + 1, 0, 23, 59, 59)
      break
      
    case 'lastMonth':
      start = new Date(year, month - 1, 1, 0, 0, 0)
      end = new Date(year, month, 0, 23, 59, 59)
      break
      
    case 'thisYear':
      start = new Date(year, 0, 1, 0, 0, 0)
      end = new Date(year, 11, 31, 23, 59, 59)
      break
      
    case 'lastYear':
      start = new Date(year - 1, 0, 1, 0, 0, 0)
      end = new Date(year - 1, 11, 31, 23, 59, 59)
      break
      
    default:
      start = new Date(year, month, date, 0, 0, 0)
      end = new Date(year, month, date, 23, 59, 59)
  }
  
  return [start, end]
}